package jp.tkym.labs.client;

import java.util.Iterator;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HeaderPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Labs implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	private final Messages messages = GWT.create(Messages.class);
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final DockPanel dockPanel = new DockPanel();
		dockPanel.add(buildMenuBar(),  DockPanel.NORTH);
		dockPanel.add(buildFileTreePanel(),  DockPanel.WEST);
		dockPanel.add(buildMainViewerPanel(),  DockPanel.CENTER);
//		dockPanel.setHeight(Window.getClientHeight());
//		dockPanel.setWidth("100 PCT");
		RootPanel.get("dockPanelContainer").add(dockPanel);
	}
	
	private Panel buildMainViewerPanel(){
		DockPanel panel = new DockPanel();
		TabBar tabBar = new TabBar();
		tabBar.addTab("text");
		tabBar.addTab("form");
		tabBar.addTab("viewer");
		panel.add(tabBar, DockPanel.NORTH);
		HTML dummy = new HTML("--------------------------------------------------------------------------------------------------------------------------------------");
		panel.add(dummy, DockPanel.CENTER);
		return panel;
	}
	
	/**
	 * 
	 * @return
	 */
	private Panel buildFileTreePanel(){
		DockPanel dockPanel = new DockPanel();
		
		HTML importPanel = new HTML();
		importPanel.setText("import");
		importPanel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				fileUploadDialog.show();
			}
		});
		
		dockPanel.add(buildFileTree(), DockPanel.CENTER);
		HeaderPanel headerPanel = new HeaderPanel();
		headerPanel.setTitle("header ....");
		headerPanel.add(dockPanel);
		
		return dockPanel;
	}
	
	private Tree buildFileTree(){
		// build TreeItem
		TreeItem rootItem = new TreeItem();
		rootItem.setText("uke files");
		
		TreeItem item01 = new TreeItem();
		item01.setText("syaho 2011/10");
		rootItem.addItem(item01);
		
		TreeItem item02 = new TreeItem();
		item02.setText("kokuho 2011/10");
		rootItem.addItem(item02);
		
		Tree tree = new Tree();
		tree.addItem(rootItem);
		return tree;
	}
	
	/**
	 * 
	 */
	private DialogBox buildFileUploadDialogBox(){
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setAnimationEnabled(true);
		dialogBox.setAutoHideEnabled(true);
		dialogBox.setTitle("drop uke file");
		dialogBox.setText("drop uke file");
		dialogBox.center();
		// DialogBox is a SimplePanel, so you have to set its widget property to
		// whatever you want its contents to be.
		Button close = new Button("close");
		close.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
		dialogBox.add(close);
		return dialogBox;
	}
	
	private DialogBox fileUploadDialog = buildFileUploadDialogBox();
	
	/**
	 * build Menubar instance
	 * @return
	 */
	MenuBar buildMenuBar(){
		final MenuBar menuBar = new MenuBar();
		Command command01 = new Command() {
			public void execute() {
				fileUploadDialog.show();
			}
		};
		final MenuItem item01 = new MenuItem("command01", command01);
		menuBar.addItem(item01);
		Command command02 = new Command() {
			public void execute() {
				Window.alert("command02");
			}
		};
		final MenuItem item02 = new MenuItem("command02", command02);
		menuBar.addItem(item02);
		return menuBar;
	}
}